<script lang="ts" setup>
import { ref, onBeforeMount, onMounted, onUnmounted } from "vue";
import type { TabsPaneContext } from "element-plus";
import { OfficeBuilding } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";

/*职位信息接口定义*/
interface Job {
    id: string;
    title: string;
    salary: string;
    location: string;
    education: string;
    company: string;
    industry: string;
    tags: string[];
    isSchool: boolean;
}

/**
 * 分类接口定义
 */
interface Category {
    label: string;
    value: string;
}

// 组件props定义
const props = defineProps<{
    jobs?: Job[]; // 职位列表数据
    categories?: Category[]; // 分类数据
    activeCategory?: string; // 当前选中分类
}>();

// 组件事件定义
const emit = defineEmits<{
    (e: "tabClick", tab: TabsPaneContext): void; // 分类标签点击事件
    (e: "viewMore"): void; // 查看更多点击事件
}>();

// 默认分类数据
const defaultCategories = ref([
    { label: "综合", value: "all" },
    { label: "管培生", value: "management" },
    { label: "教师", value: "teacher" },
    { label: "银行", value: "bank" },
    { label: "国企", value: "state" },
    { label: "外企", value: "foreign" },
]);

// 默认职位数据
const defaultJobs = ref([
    {
        id: "1",
        title: "综合管理类",
        salary: "8-12K",
        location: "天津",
        education: "本科",
        company: "正邦集团",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "管培生"],
        isSchool: true,
    },
    {
        id: "2",
        title: "高中一对一物理老师",
        salary: "10-15K",
        location: "天津",
        education: "本科",
        company: "天津新东方",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "教师"],
        isSchool: true,
    },
    {
        id: "3",
        title: "国企 管培生 人事行政文员",
        salary: "6-10K",
        location: "天津",
        education: "本科",
        company: "中国人寿",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "双休", "六日双休"],
        isSchool: true,
    },
    {
        id: "4",
        title: "利安隆新材料招聘生产安全员",
        salary: "6-8K·13薪",
        location: "天津",
        education: "本科",
        company: "利安隆新材料",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "安全专业"],
        isSchool: true,
    },
    {
        id: "5",
        title: "销售管培生（B端销售）",
        salary: "10-15K",
        location: "天津",
        education: "本科",
        company: "欧麦食品股份有限公司",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "休闲/餐饮"],
        isSchool: true,
    },
    {
        id: "6",
        title: "（悦动招聘）数据治理工程师",
        salary: "12-15K",
        location: "天津",
        education: "硕士",
        company: "北京爱梯科",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "计算机/软件"],
        isSchool: true,
    },
    {
        id: "7",
        title: "青英计划（销售）J12487",
        salary: "7-10K",
        location: "天津",
        education: "本科",
        company: "青岛啤酒股份有限公司",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "六险三金", "国企"],
        isSchool: true,
    },
    {
        id: "8",
        title: "银行2025届优才生培训",
        salary: "8-13K·13薪",
        location: "天津",
        education: "本科",
        company: "招商银行信用卡中心",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "企业（To B）"],
        isSchool: true,
    },
    {
        id: "9",
        title: "有机合成研究员",
        salary: "7-12K·13薪",
        location: "天津",
        education: "本科",
        company: "新美薬研新药集团",
        industry: "已上市",
        tags: ["在校/应届", "五险一金", "医疗/健康"],
        isSchool: true,
    },
]);

// 实际展示的数据
const categories = ref(props.categories || defaultCategories.value);
const jobs = ref(props.jobs || defaultJobs.value);
const activeCategory = ref(props.activeCategory || "all");

// 路由实例
const router = useRouter(); // 获取路由实例

/**
 * 分类标签点击处理
 * @param tab - 被点击的标签页上下文
 */
const handleTabClick = (tab: TabsPaneContext) => {
    emit("tabClick", tab); // 触发tabClick事件
};

/**
 * 查看更多按钮点击处理
 */
const handleViewMore = () => {
    router.push("/job"); // 跳转到职位列表页
};

/**
 * 跳转到职位详情页
 * @param id - 职位ID
 */
const gotoJobDetail = (id: string) => {
    router.push({
        path: "/schoolHireInfo",
        query: { id },
    });
};

//生命周期钩子
onBeforeMount(() => {});
onMounted(() => {});
onUnmounted(() => {});
</script>

<template>
    <!-- 热门校招模块 -->
    <div class="hot-school-jobs">
        <!-- 模块标题 -->
        <h2 class="section-title">热门校招</h2>

        <!-- 分类标签页 -->
        <el-scrollbar>
            <el-tabs
                :model-value="activeCategory"
                class="demo-tabs"
                @update:model-value="(val) => emit('tabClick', { props: { name: val } } as TabsPaneContext)"
            >
                <!-- 遍历分类数据 -->
                <el-tab-pane v-for="item in categories" :key="item.value" :label="item.label" :name="item.value" />
            </el-tabs>
        </el-scrollbar>

        <!-- 职位列表 -->
        <div class="job-cards">
            <el-row :gutter="20">
                <el-col v-for="job in jobs" :key="job.id" :xs="24" :sm="12" :md="8" :lg="6" class="job-column">
                    <div class="job-card" @click="gotoJobDetail(job.id)">
                        <!-- 职位卡片内容 -->
                        <div class="job-header">
                            <div class="job-title">{{ job.title }}</div>
                            <div class="job-salary">{{ job.salary }}</div>
                        </div>
                        <!-- 职位要求 -->
                        <div class="job-requirements">
                            <span>{{ job.location }}</span>
                            <el-divider direction="vertical" />
                            <span>在校/应届</span>
                            <el-divider direction="vertical" />
                            <span>{{ job.education }}</span>
                        </div>
                        <!-- 公司信息 -->
                        <div class="company-info">
                            <div class="company-left">
                                <el-icon class="company-icon"><OfficeBuilding /></el-icon>
                                <span>{{ job.company }}</span>
                            </div>
                            <div class="company-right">
                                <!-- 融资阶段标签 -->
                                <el-tag class="financing-tag" type="success" size="small">
                                    {{ job.industry }}
                                </el-tag>
                            </div>
                        </div>
                    </div>
                </el-col>
            </el-row>
        </div>

        <!-- 查看更多按钮 -->
        <div class="view-more-container">
            <el-button class="view-more" @click="handleViewMore">查看更多</el-button>
        </div>
    </div>
</template>

<style lang="less" scoped>
.hot-school-jobs {
    width: 100%;
    padding: 20px 0;

    /* 模块标题样式 */
    .section-title {
        font-size: 24px;
        font-weight: 600;
        margin-bottom: 20px;
        position: relative;
        padding-left: 15px;

        &:before {
            content: "";
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 20px;
            background-color: #409eff;
            border-radius: 2px;
        }
    }

    /* 职位列表样式 */
    .job-cards {
        margin: 20px 0;
    }

    /* 职位卡片列样式 */
    .job-column {
        margin-bottom: 20px;
    }

    /* 职位卡片样式 */
    .job-card {
        height: 100%;
        transition: all 0.3s;
        border-radius: 8px;

        /* 悬停效果 */
        &:hover {
            transform: translateY(-5px);
            box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
        }

        /* 卡片头部样式 */
        .job-header {
            padding: 12px 0;

            /* 职位标题样式 */
            .job-title {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 8px;

                span:first-child {
                    font-size: 16px;
                    font-weight: 600;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    max-width: 65%;
                }

                /* 薪资样式 */
                .job-salary {
                    color: #f56c6c;
                    font-weight: bold;
                    font-size: 16px;
                }
            }
        }

        /* 职位要求样式 */
        .job-requirements {
            color: #999;
            font-size: 12px;
            display: flex;
            align-items: center;

            :deep(.el-divider) {
                margin: 0 8px;
                background-color: #ddd;
            }
        }

        /* 公司信息样式 */
        .company-info {
            padding: 12px 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 14px;

            .company-left {
                display: flex;
                align-items: center;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                max-width: 70%;

                .el-icon {
                    margin-right: 8px;
                    color: #409eff;
                    font-size: 16px;
                }
            }

            .company-right {
                display: flex;
                align-items: center;
            }
        }
    }

    /* 查看更多按钮容器 */
    .view-more-container {
        text-align: center;
        margin-top: 20px;
    }

    /* 查看更多按钮样式 */
    .view-more {
        padding: 10px 30px;
        font-size: 14px;
        background-color: #f5f7fa;
        color: #606266;
        border: none;
        transition: all 0.3s;

        &:hover {
            background-color: #e6ebf5;
        }
    }
}

/* 移动端适配 */
@media (max-width: 768px) {
    .hot-school-jobs {
        padding: 30px 15px;

        .section-title {
            font-size: 20px;
        }

        .job-column {
            margin-bottom: 15px;
        }
    }
}
</style>
